﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Hont.HyperWigglePackage.Editor
{
    public partial class HyperWiggleEditorView
    {
        CommonVO mCommonVO;


        void InitCommonPartial()
        {
            mCommonVO = new CommonVO();
        }

        public void UpdateCommon(CommonVO commonVO)
        {
            mCommonVO = commonVO;
        }

        void RefreshCommon()
        {
            var icon = EditorGUIUtility.FindTexture("GameManager Icon");

            if (Event.current.type == EventType.DragExited && DragAndDrop.objectReferences.Length > 0)
            {
                var dragItem = DragAndDrop.objectReferences[0];
                if (mCommonVO.OnQuickLoadBtnClickedCB != null)
                {
                    mCommonVO.OnQuickLoadBtnClickedCB(dragItem);
                    DragAndDrop.objectReferences = new Object[0];
                    DragAndDrop.AcceptDrag();
                    DragAndDrop.PrepareStartDrag();
                    Event.current.Use();
                    EditorUtility.DisplayDialog("Tip", "Loaded!", "ok");

                    return;
                }
            }

            if (GUI.Button(new Rect(0, 0, 40, 40), icon))
            {
                var genericMenu = new GenericMenu();
                genericMenu.AddItem(new GUIContent("New"), false, () =>
                {
                    if (mCommonVO.OnNewBtnClickedCB != null)
                        mCommonVO.OnNewBtnClickedCB();
                });
                genericMenu.AddItem(new GUIContent("Save"), false, () =>
                {
                    if (mCommonVO.OnSaveBtnClickedCB != null)
                        mCommonVO.OnSaveBtnClickedCB();
                });
                genericMenu.AddItem(new GUIContent("Load"), false, () =>
                {
                    if (mCommonVO.OnLoadBtnClickedCB != null)
                        mCommonVO.OnLoadBtnClickedCB();
                });

                genericMenu.AddSeparator("");

                genericMenu.AddItem(new GUIContent("Loop Preview"), mCommonVO.LoopPreview, () =>
                {
                    if (mCommonVO.OnLoopPreviewToggleChangedCB != null)
                        mCommonVO.OnLoopPreviewToggleChangedCB(!mCommonVO.LoopPreview);
                });

                genericMenu.AddSeparator("");
                for (int i = 0; i < mCommonVO.CustomButtonsArray.Length; i++)
                {
                    var item = mCommonVO.CustomButtonsArray[i];
                    var j = i;
                    genericMenu.AddItem(item, false, () =>
                    {
                        if (mCommonVO.OnCustomButtonClickedCB != null)
                            mCommonVO.OnCustomButtonClickedCB(j);
                    });
                }

                genericMenu.ShowAsContext();
            }
        }
    }
}
